﻿function colorClass2(a) {
    var f1 = parseFloat(a);

    if (f1 > 0) {
        return "cr";
    }
    else if (f1 < 0) {
        return "cgren";
    }
    else {
        return "";
    }
}

//d 判断值 p正数时样式 n负数时样式 z零时样式
function formatColor(d, p, n, z) {
    var d = parseFloat(d);

    if (d > 0) {
        return p;
    }
    else if (d < 0) {
        return n;
    }
    else {
        return z;
    }
}

function formatFloat(v, f) {
    try {
        if (f == undefined || f == "") {
            f = 2;
        }
        return parseFloat(v).toFixed(f);
    } catch (e) {
        return 0;
    }
}

function clineFontColor(a) {
    var f1 = parseFloat(a);

    if (f1 >= 0) {
        return "cy4";
    }
    else {
        return "cblue";
    }
}

function formatCline(cline)
{
    var intCline = parseInt(cline);

    if (intCline > 0) {
        if (intCline == 1) {
            return "B";
        }
        else {
            return "B" + intCline;
        }
    }
    else {
        if (intCline == -1) {
            return "S";
        }
        else {
            return "S" + Math.abs(intCline);
        }
    }
}

// parse Date from the service date format
function parseFullDate(jsonDate) {
    var date = (new Date(+/\d+/.exec(jsonDate)));
    var month = parseInt(date.getMonth()) + 1;
    var day = parseInt(date.getDate());
    var hour = parseInt(date.getHours());
    var minute = parseInt(date.getMinutes());
    var second = parseInt(date.getSeconds());

    return date.getFullYear() + "-" + (month > 9 ? month : "0" + month) + "-" + (day > 9 ? day : "0" + day) + " "
        + (hour > 9 ? hour : "0" + hour) + ":" + (minute > 9 ? minute : "0" + minute) + ":" + (second > 9 ? second : "0" + second);
}

function parseDate(jsonDate, format) {
    var re = /(\d+-\d+-\d+)[T|\s](\d+:\d+:\d+)/ig;
    r = re.exec(jsonDate)
    
    if (r.length == 3) {
        switch (format) {
            case "yyyy-MM-dd HH:mm:ss":
                return r[1] + " " + r[2];
            case "yyyyMMdd":
                return r[1].replace(/-/g, "");
            default:
                return r[1];
        }
    }
    else {
        return jsonDate;
    }
    //var date;
    ////alert(1);
    //if (jsonDate.indexOf("T") > -1) {
    //    //alert(jsonDate.replace("T", " "));
    //    //date = new Date("2014/6/18");
    //    //alert(date);
    //}
    //else {
    //    date = (new Date(+/\d+/.exec(jsonDate)));
    //}

    //var month = parseInt(date.getMonth()) + 1;
    //var day = parseInt(date.getDate());
    
    //switch (format) {
    //    case "M-d":
    //        return month + "-" + day;
    //    case "MM-dd":
    //        return (month > 9 ? month : "0" + month) + "-" + (day > 9 ? day : "0" + day);
    //    default:
    //        return date.getFullYear() + "-" + (month > 9 ? month : "0" + month) + "-" + (day > 9 ? day : "0" + day);
    //}
}

function compareDate(dt1, dt2)
{
    return dt1 > dt2;
}

function formatLongText(str, n)
{
    if (str != undefined && str != "" && str.length > n) {
        var tmpStr = str.substr(0, n);
        var tmpCode = tmpStr.replace(/[^\x00-\xff]/g, '\r\n').split('');
        n = (tmpCode[n - 1] == '\r') ? n - 2 : n - 1;
        var l = tmpCode.slice(0, n).join('').replace(/\r\n/g, '*').length + 1;

        return tmpStr.substr(0, l);
    }
    else {
        return str;
    }
}

function addCookie(name, value, expiresHours) {
    var cookieString = name + "=" + escape(value);
    //判断是否设置过期时间 
    if (expiresHours > 0) {
        var date = new Date();
        date.setTime(date.getTime + expiresHours * 3600 * 1000);
        cookieString = cookieString + "; expires=" + date.toGMTString();
    }
    document.cookie = cookieString;
}

function getCookie(name) {
    var strCookie = document.cookie;
    
    var arrCookie = strCookie.split("; ");
    for (var i = 0; i < arrCookie.length; i++) {
        var arr = arrCookie[i].split("=");
        if (arr[0] == name) return arr[1];
    }
    return "";
}

function deleteCookie(name) {
    var date = new Date();
    date.setTime(date.getTime() - 10000);
    document.cookie = name + "=v; expires=" + date.toGMTString();
}

//fixed ieBug
(function ($, h, c) { var a = $([]), e = $.resize = $.extend($.resize, {}), i, k = "setTimeout", j = "resize", d = j + "-special-event", b = "delay", f = "throttleWindow"; e[b] = 250; e[f] = true; $.event.special[j] = { setup: function () { if (!e[f] && this[k]) { return false } var l = $(this); a = a.add(l); $.data(this, d, { w: l.width(), h: l.height() }); if (a.length === 1) { g() } }, teardown: function () { if (!e[f] && this[k]) { return false } var l = $(this); a = a.not(l); l.removeData(d); if (!a.length) { clearTimeout(i) } }, add: function (l) { if (!e[f] && this[k]) { return false } var n; function m(s, o, p) { var q = $(this), r = $.data(this, d); r.w = o !== c ? o : q.width(); r.h = p !== c ? p : q.height(); n.apply(this, arguments) } if ($.isFunction(l)) { n = l; return m } else { n = l.handler; l.handler = m } } }; function g() { i = h[k](function () { a.each(function () { var n = $(this), m = n.width(), l = n.height(), o = $.data(this, d); if (m !== o.w || l !== o.h) { n.trigger(j, [o.w = m, o.h = l]) } }); g() }, e[b]) } })(jQuery, this);
$(function () {
    $.fn.extend({
        followTips: function (tipinf) {
            var self = this
            , t = $(".qatip")
            , vp = 0
            , vx = {
                mousePos: function (e) {
                    var x, y;
                    var e = e || window.event;
                    return {
                        x: e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
                        y: e.clientY + document.body.scrollTop + document.documentElement.scrollTop,
                        p: e.clientX
                    };
                }
            };
            $(this).mousemove(function (e) {
                var mouse = vx.mousePos(e)
                    , vpx = (($(window).width() - mouse.x) < t.outerWidth()) ? (0 - t.outerWidth() - 20) : 0
                    , vpy = (($(window).height() - mouse.y) < t.outerWidth()) ? (0 - t.outerHeight() - 20) : 0;
                t.css({ 'left': (mouse.x + 10 + vpx + 'px'), 'top': (mouse.y + 10 + vpy + 'px') })
                    .html($(this).attr("event"))
                    .show();
            });
            $(this).bind("mouseup mouseout", function () {
                t.hide();
            });
        }
    });
});

function colorcss(d1, d2) {
    d1 = d1 + "";
    if (d1.indexOf("%") > -1) {
        d1 = d1.replace("%", "");
    }

    d1 = formatFloat(d1);
    d2 = formatFloat(d2);

    if (d1 > d2) {
        return "red";
    }
    else if (d1 < d2) {
        return "green";
    }
    else {
        return "";
    }
}
function formatFloat(v, f) {
    try {
        v = v + "";
        if (v.indexOf("--") != -1) {
            return v;
        }
        if (v.indexOf("%") > -1) {
            v = v.replace("%", "");
        }
        if (f == undefined || f == "") {
            f = 2;
        }
        return parseFloat(v).toFixed(f);
    } catch (e) {
        return 0;
    }
}